package cn.iocoder.yudao.module.im.controller.admin.member;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.im.controller.admin.member.vo.*;
import cn.iocoder.yudao.module.im.dal.dataobject.member.MemberConversationDO;
import cn.iocoder.yudao.module.im.service.member.MemberConversationService;

@Tag(name = "管理后台 - IM会话")
@RestController
@RequestMapping("/im/member-conversation")
@Validated
public class MemberConversationController {

    @Resource
    private MemberConversationService memberConversationService;

    @PostMapping("/create")
    @Operation(summary = "创建IM会话")
    @PreAuthorize("@ss.hasPermission('im:member-conversation:create')")
    public CommonResult<Long> createMemberConversation(@Valid @RequestBody MemberConversationSaveReqVO createReqVO) {
        return success(memberConversationService.createMemberConversation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新IM会话")
    @PreAuthorize("@ss.hasPermission('im:member-conversation:update')")
    public CommonResult<Boolean> updateMemberConversation(@Valid @RequestBody MemberConversationSaveReqVO updateReqVO) {
        memberConversationService.updateMemberConversation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除IM会话")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('im:member-conversation:delete')")
    public CommonResult<Boolean> deleteMemberConversation(@RequestParam("id") Long id) {
        memberConversationService.deleteMemberConversation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得IM会话")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('im:member-conversation:query')")
    public CommonResult<MemberConversationRespVO> getMemberConversation(@RequestParam("id") Long id) {
        MemberConversationDO memberConversation = memberConversationService.getMemberConversation(id);
        return success(BeanUtils.toBean(memberConversation, MemberConversationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得IM会话分页")
    @PreAuthorize("@ss.hasPermission('im:member-conversation:query')")
    public CommonResult<PageResult<MemberConversationRespVO>> getMemberConversationPage(@Valid MemberConversationPageReqVO pageReqVO) {
        PageResult<MemberConversationDO> pageResult = memberConversationService.getMemberConversationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberConversationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出IM会话 Excel")
    @PreAuthorize("@ss.hasPermission('im:member-conversation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMemberConversationExcel(@Valid MemberConversationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberConversationDO> list = memberConversationService.getMemberConversationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "IM会话.xls", "数据", MemberConversationRespVO.class,
                        BeanUtils.toBean(list, MemberConversationRespVO.class));
    }

}